using UnityEngine;

public class EnergyToCoinsConverter : MonoBehaviour
{
	public int energyCost = 10;

	public SeasonTournamentLayer tournamentLayer;

	public UILabel title;

	public bool alwaysWin;

	public bool alwaysLose;

	private void Update()
	{
		if (CanPlay())
		{
			string text = "Play";
			if (alwaysWin != alwaysLose)
			{
				text = ((!alwaysWin) ? "Lose" : "Win");
			}
			UITools.ChangeText(title, text);
			GetComponent<UIButton>().enabled = true;
		}
		else
		{
			UITools.ChangeText(title, "Unavailable");
			GetComponent<UIButton>().enabled = false;
		}
	}

	private bool CanPlay()
	{
		if (BehaviourSingleton<EnergyManager>.instance.GetCurrentEnergyValue() < (float)energyCost)
		{
			return false;
		}
		return true;
	}

	public void Convert()
	{
		BehaviourSingleton<EnergyManager>.instance.SpendEnergy(energyCost);
		if (IsWinner())
		{
			GGPlayerSettings.instance.walletManager.AddCurrency(CurrencyType.coins, tournamentLayer.activeTier.coinsForWin);
		}
		GGPlayerSettings instance = GGPlayerSettings.instance;
		LevelUpLayer.LevelProgressInfo levelProgressInfo = default(LevelUpLayer.LevelProgressInfo);
		levelProgressInfo.levelProgress = instance.LevelProgress();
		levelProgressInfo.levelName = instance.LevelName();
		LevelUpLayer.LevelProgressInfo oldLevel = levelProgressInfo;
		levelProgressInfo = default(LevelUpLayer.LevelProgressInfo);
		levelProgressInfo.levelProgress = instance.LevelProgress();
		levelProgressInfo.levelName = instance.LevelName();
		LevelUpLayer.LevelProgressInfo newLevel = levelProgressInfo;
		LevelUpLayer.instance.DoShowLevelProgress(oldLevel, newLevel);
	}

	private bool IsWinner()
	{
		if (alwaysWin)
		{
			return true;
		}
		if (alwaysLose)
		{
			return false;
		}
		float value = Random.value;
		return value < 0.5f;
	}
}
